package com.anplurk.model;

import java.util.Hashtable;
import java.util.logging.Logger;

import com.anplurk.api.PlurkAPI;

public class PlurkSession {

    private Logger logger = Logger.getLogger("PlurkSession");
    private UserModel myProfile = null;
    private Hashtable<String, UserModel> myFriends = null;
    private Hashtable<String, UserModel> myFans = null;
    private Hashtable<String, UserModel> myOthers = null;
     
    private boolean isLogin = false;
    
    
    public PlurkSession() {
         myProfile = new UserModel();
         myFriends = new Hashtable<String, UserModel>();      
         myFans = new Hashtable<String, UserModel>();
         myOthers = new Hashtable<String, UserModel>();      
    }
    
    public UserModel getMyProfile() {
        return myProfile;
    }
    public Hashtable<String, UserModel> getMyFriends() {
        return myFriends;
    }
    public void setMyProfile(UserModel myProfile) {
        this.myProfile = myProfile;
    }
    public void setMyFriends(Hashtable<String, UserModel> friends) {
        this.myFriends = friends;
    }    
    
    public UserModel getFriend(String uid) {
        return myFriends.get(uid);
    }
    
    public Hashtable<String, UserModel> getMyFans() {
        return myFans;
    }

    public void setMyFans(Hashtable<String, UserModel> myFans) {
        this.myFans = myFans;
    }
    
    public UserModel getFan(String uid) {
        return myFans.get(uid);
    }
    
    public Hashtable<String, UserModel> getMyOthers() {
        return myOthers;
    }

    public void setMyOthers(Hashtable<String, UserModel> myOthers) {
        this.myOthers = myOthers;
    }
    
    public UserModel getOther(String uid) {
        return myOthers.get(uid);
    }       

    public UserModel findUserByUId(String uid) {
        
        if (this.getMyProfile().getUid().equals(uid)) {
            logger.info(uid + " is me" );
            return this.getMyProfile();
        }
        else if (this.getFriend(uid) != null) {
            logger.info(uid  + " is friend" );
            return this.getFriend(uid);
        }
        else if (this.getFan(uid) != null) {
            logger.info(uid  + " is fan" );
            return this.getFan(uid);
        }
        else if (this.getOther(uid) != null) {
            logger.info(uid  + " is other" );
            return this.getOther(uid);
        }        
        else {            
            UserModel user = new UserModel();
            try {
                user = PlurkAPI.getInstance().findUserByUid(uid);
                this.getMyOthers().put(user.getUid(), user);
            } catch (Exception e) {
                logger.warning(uid + " is an invalid user!!");
                user.setUid(uid);
                user.setAvatarSrc("http://a.b.c");
                user.setNickname("unknown");
            }

            return user;
        }
    }
    
    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }        
    

}    


